package service4gateway

import (
	Article "MyBlogv2/Servers/Article/article"
	"MyBlogv2/blog-common/HttpCode"
	"MyBlogv2/blog-common/helper"
	"MyBlogv2/blog-common/models"
	"MyBlogv2/blog-common/response"
	"github.com/gin-gonic/gin"
	"github.com/jinzhu/copier"
	"strconv"
	"time"
)

func GetTagList(c *gin.Context) {
	rsp, _ := clientOfArticle.GetTagList(c, &Article.ArticleRequest{})
	var tagVoList []models.TagVo
	copier.Copy(&tagVoList, rsp.TagVoList)
	for i, v := range rsp.TagVoList {
		tagVoList[i].CreateTime = helper.TimestampToMyTime(v.CreateTime)
		tagVoList[i].UpdateTime = helper.TimestampToMyTime(v.UpdateTime)
	}
	response.ResponseOK(c, tagVoList)
	return
}

func AddTag4Article(c *gin.Context) {
	var tagDto models.TagDto
	if err := c.ShouldBindJSON(&tagDto); err != nil {
		response.ResponseError(c, HttpCode.PARAM_ERROR)
	}
	tagDto.Id = 0
	models.Db.Model(new(models.TagDto)).Create(&tagDto)
	response.ResponseOK(c, nil)
	return
}

func GetArticleTagList(c *gin.Context) {
	var tagVoList []models.TagVo
	models.Db.Model(new(models.TagVo)).Find(&tagVoList)
	for i, v := range tagVoList {
		var articleCount int64
		models.Db.Model(new(models.ArticleTag)).Where("tag_id = ?", v.Id).Count(&articleCount)
		tagVoList[i].ArticleCount = int(articleCount)
	}
	response.ResponseOK(c, tagVoList)
	return
}

func SearchTag(c *gin.Context) {
	var searchTagDto models.SearchTagDto
	if err := c.ShouldBindJSON(&searchTagDto); err != nil {
		response.ResponseError(c, HttpCode.PARAM_ERROR)
	}
	var tagVoList []models.TagVo
	db := models.Db.Model(new(models.TagVo))
	if searchTagDto.TagName != "" {
		db.Where("tag_name like ?", "%"+searchTagDto.TagName+"%")
	}
	if !time.Time(searchTagDto.StartTime).IsZero() {
		db.Where("created_at >= ?", searchTagDto.StartTime)
	}
	if !time.Time(searchTagDto.EndTime).IsZero() {
		db.Where("created_at <= ?", searchTagDto.EndTime)
	}
	db.Find(&tagVoList)
	for i, v := range tagVoList {
		var articleCount int64
		models.Db.Model(new(models.ArticleTag)).Where("tag_id = ?", v.Id).Count(&articleCount)
		tagVoList[i].ArticleCount = int(articleCount)

	}
	response.ResponseOK(c, tagVoList)
	return
}

func GetTagById(c *gin.Context) {
	id, _ := strconv.Atoi(c.Param("id"))
	var tagVo models.TagVo
	models.Db.Model(new(models.TagVo)).Where("id = ?", id).First(&tagVo)
	var articleCount int64
	models.Db.Model(new(models.ArticleTag)).Where("tag_id = ?", id).Count(&articleCount)
	tagVo.ArticleCount = int(articleCount)
	response.ResponseOK(c, tagVo)
	return
}

func AddOrUpdateTag(c *gin.Context) {
	var tagDto models.TagDto
	if err := c.ShouldBindJSON(&tagDto); err != nil {
		response.ResponseError(c, HttpCode.PARAM_ERROR)
	}
	if tagDto.Id == 0 {
		models.Db.Model(new(models.TagDto)).Create(&tagDto)
	} else {
		models.Db.Model(new(models.TagDto)).Where("id = ?", tagDto.Id).Updates(&tagDto)
	}
	response.ResponseOK(c, nil)
	return
}

func DeleteTagByIds(c *gin.Context) {
	var ids []int
	if err := c.ShouldBindJSON(&ids); err != nil {
		response.ResponseError(c, HttpCode.PARAM_ERROR)
	}
	var articleCount int64
	models.Db.Model(new(models.ArticleTag)).Where("tag_id in (?)", ids).Count(&articleCount)
	if articleCount > 0 {
		response.CommonResponse(c, HttpCode.FAILURE, nil, "存在文章引用该标签，无法删除")
		return
	}
	models.Db.Model(new(models.Tag)).Where("id in (?)", ids).Delete(new(models.Tag))
	response.ResponseOK(c, nil)
	return

}
